import QtQuick 2.0
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import QtWebEngine 1.5
import '../../widget'
import '../../labels'

StackPage {
    id:window
    tabIndex:0
    tabsVisible: false
    property double lat;
    property double lng;
    ColumnLayout{
        anchors.fill: parent
        spacing:0
        Rectangle {
            Layout.fillWidth: true
            Layout.preferredHeight: 40
            RowLayout{
                anchors.fill:parent
                ReturnImg{
                    Layout.preferredWidth:20
                    onClicked: {
                        window.pop();
                    }
                }
                TitleLabel {
                    Layout.fillWidth: true
                    text: qsTr("地点微调")
                }
                ContentLabel{
                    text:"确定"
                    color:"#2A9CFF"
                    Layout.preferredWidth:35
                }
            }
        }

        WebEngineView{
            id:webEngineView
            Layout.fillWidth: true
            Layout.preferredHeight: 300
            Layout.alignment:Qt.AlignHCenter
            url:"qrc:/maps/poi.html?lat="+window.lat+"&lng="+window.lng
            onLoadingChanged: {
                if(!webEngineView.loading){
                    runJavaScript("getResult()",function(result){
                        var res = JSON.parse(result)
                        var title,place
                        for(var i in res){
                            var r = res[i]
                            title = r.name
                            place = r.pname+r.cityname+r.adname+r.address
                            listModel.append({"title":title,"place":place})
                        }
                    })
                }
            }
        }

        ScrollView{
            id:scrollView
            Layout.fillWidth:true
            Layout.fillHeight: true
            ScrollBar.vertical: ScrollBar {
                parent: scrollView
                x: scrollView.mirrored ? 0 : scrollView.width - width
                y: scrollView.topPadding
                height: scrollView.availableHeight
                active: scrollView.ScrollBar.horizontal.active
            }

            ScrollBar.horizontal: ScrollBar {
                parent: scrollView
                x: scrollView.leftPadding
                y: scrollView.height - height
                width: scrollView.availableWidth
                active: scrollView.ScrollBar.vertical.active
            }
            background: Rectangle {
                border.color: "#FFFFFF"
                color:"#F8F8F8"
            }
            ListView{
                id:listView
                model:ListModel {
                    id:listModel
                }

                delegate: Rectangle {
                    id:listDelegate
                    function isCurrentIndex(){
                        return ListView.isCurrentItem
                    }
                    width:scrollView.width
                    height:80
                    ColumnLayout{
                        anchors.fill:parent
                        Rectangle{
                            Layout.fillWidth: true
                            Layout.preferredHeight: 20
                            Layout.topMargin: 20
                            ContentLabel{
                                text:title
                                anchors{
                                    left:parent.left
                                    leftMargin:15
                                    verticalCenter: parent.verticalCenter
                                }
                            }



                            Image{
                                id:complete
                                visible:listDelegate.isCurrentIndex()
                                source:"qrc:/img/complete.png"
                                sourceSize: Qt.size(20,20)
                                anchors{
                                    right:parent.right
                                    rightMargin:15
                                    verticalCenter: parent.verticalCenter
                                }
                            }


                        }
                        Rectangle{
                            Layout.fillWidth: true
                            Layout.preferredHeight: 20
                            ContentLabel{
                                text:place
                                color:"#B1B1B1"
                                anchors{
                                    left:parent.left
                                    leftMargin:15
                                    verticalCenter: parent.verticalCenter
                                }
                            }
                        }
                        Rectangle{
                            Layout.fillWidth:true
                            Layout.fillHeight: true
                        }
                        Rectangle{
                            color:'#C1C1C1'
                            Layout.fillWidth: true
                            Layout.preferredHeight: 1
                        }
                    }
                    MouseArea{
                        anchors.fill:parent
                        onClicked:{
                            listView.currentIndex = index
                        }
                    }
                }
            }
        }
    }
}
